<!-- 投标者的登录和注册 -->
<script setup>
import { ref } from 'vue';
import { ElForm, ElFormItem, ElInput, ElButton, ElCard, ElContainer } from 'element-plus';
import { ElMessage } from 'element-plus'
const islogin=ref(true)

// 表单数据模型
const registerDate = ref({
  username: '',
  password: '',
  confirmPassword: '',
  companyName: '',
  name:'',
  phone: '',
  address: '',
});
// 表单数据模型
const loginDate = ref({
    username: '',
    password: '',
  });

// 验证确认密码一致性
const validateConfirmPassword = (rule, value, callback) => {
  if (value !== registerDate.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

// 表单验证规则
const rules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符之间', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  companyName: [
    { required: true, message: '请输入投标单位名称', trigger: 'blur' },
  ],
  name: [
  { required: true, message: '请输入联系人姓名', trigger: 'blur' },
],
phone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { type: 'phone', message: '请输入有效的电话', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入地址', trigger: 'blur' },
  ],
});
const rules2 = ref({
    username: [
      {required: true,message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
      {required: true,message: '请输入密码', trigger: 'blur' },
    ],

  });

import { tendererResgisterService,tendereLogin,getTenderer} from '@/api/tenderer.js';

// 提交表单
const register = async () => {
  let result = await tendererResgisterService(registerDate.value);
  if (result.code === 200) {
    ElMessage.success("注册成功");
  } else {
    ElMessage.error("注册失败");
  }
};
import userInfoStore from '@/stores/userInfo.js'
import { useRouter } from 'vue-router';

const router =useRouter();
const userStore=userInfoStore();
import useTokenStore from '@/stores/token.js';
const tokenStore = useTokenStore();

const login = async () => {
    let result = await tendereLogin(loginDate.value);
    if (result.code === 200) {
      ElMessage.success("登入成功");
      tokenStore.setToken(result.data)
      let info= await getTenderer(loginDate.value.username)

      userStore.setInfo(info.data);

      router.push({path:'TendererIndex'})
    } else {
      ElMessage.error("登入失败");
    }
  };
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    width="500px"
    align-center
  >
      <el-main>
        <el-card class="login-card" v-if="islogin">
          <h2 class="title">投标用户登入</h2>
          <el-form :model="loginDate" ref="formRef" :rules="rules2" label-width="110px" @submit.prevent="login">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="loginDate.username" placeholder="请输入用户名"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="loginDate.password" placeholder="请输入密码"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button @click="islogin=false">注册</el-button>
                <el-button type="primary" @click="login">登入</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <el-card class="register-card" v-else>
          <h2 class="title">投标用户注册</h2>
          <el-form :model="registerDate" ref="formRef" :rules="rules" label-width="110px" @submit.prevent="register">

            <el-form-item label="用户名" prop="username">
              <el-input v-model="registerDate.username" placeholder="请输入用户名"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input type="password" v-model="registerDate.password" placeholder="请输入密码"></el-input>
            </el-form-item>

            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input type="password" v-model="registerDate.confirmPassword" placeholder="请确认密码"></el-input>
            </el-form-item>

            <el-form-item label="投标单位名称" prop="companyName">
              <el-input v-model="registerDate.companyName" placeholder="请输入投标单位名称"></el-input>
            </el-form-item>

            <el-form-item label="法人姓名" prop="name">
            <el-input v-model="registerDate.name" placeholder="请输入法人姓名"></el-input>
            </el-form-item>

            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="registerDate.phone" placeholder="请输入联系邮箱"></el-input>
            </el-form-item>

            <el-form-item label="地址" prop="address">
              <el-input v-model="registerDate.address" placeholder="请输入地址"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button @click="islogin=true">返回</el-button>
                <el-button type="primary" @click="register">注册</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-main>
    </el-dialog>
  </template>

<style scoped>
/* 滚动条隐藏 */
::-webkit-scrollbar { width: 0; height: 0; }
::-webkit-scrollbar-track, ::-webkit-scrollbar-thumb { background: transparent; }
* { scrollbar-width: none; -ms-overflow-style: none; }

/* 基础布局 */
.login-container {
  min-height: 100vh;
  padding: 20px;
}

/* 卡片样式 */
.login-card, .register-card {
  max-width: 380px;
  border-radius: 14px;
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.08);
  padding-bottom: 30px;
  transition: all 0.3s ease;
}

.login-card:hover, .register-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 18px 35px rgba(0, 0, 0, 0.12);
}

/* 标题样式 */
.title {
  font-size: 24px;
  padding: 28px 0 20px;
  margin-bottom: 28px;
  font-weight: 600;
}

.title::after {
  width: 55px;
  height: 3px;
  background: linear-gradient(90deg, #409EFF, #79bbff);
}

/* 表单样式 */
.el-form {
  padding: 0 28px;
}

.el-form-item {
  margin-bottom: 22px;
}

.el-input {
  height: 46px;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.el-input:focus-within .el-input__inner {
  border-color: #409EFF;
  box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.15);
}

.el-input__inner {
  font-size: 15px;
  padding: 0 14px;
}

/* 按钮样式 */
.el-button {
  height: 46px;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.el-button.primary {
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}

.el-button.primary:hover {
  transform: translateY(-3px);
  box-shadow: 0 7px 18px rgba(64, 158, 255, 0.4);
}

.form-actions {
  gap: 12px;
}

/* 注册页额外字段 */
.register-fields .el-form-item {
  grid-template-columns: 100px 1fr;
  gap: 15px;
}

/* 切换按钮样式 */
.switch-btn {
  margin-top: 18px;
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 420px) {
  .login-card, .register-card {
    max-width: 90%;
    border-radius: 12px;
  }

  .title {
    font-size: 22px;
    padding: 25px 0 18px;
    margin-bottom: 25px;
  }

  .el-form {
    padding: 0 22px;
  }

  .el-input, .el-button {
    height: 44px;
  }

  .register-fields .el-form-item {
    grid-template-columns: 90px 1fr;
  }
}
</style>
